package com.tran.app.screen

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.tran.app.MyApp
import com.tran.app.viewmodel.PublicCourseViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PublicSreen(modifier: Modifier, navController: NavHostController) {
    Scaffold(modifier = Modifier.fillMaxSize(),topBar = {
        TopAppBar(title = { Text(text = "发布课程")},
            navigationIcon = {
                IconButton(onClick = { navController.popBackStack() }) {
                    Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "back")
                }
            })
    }){
        Column(
            modifier = modifier.padding(paddingValues = it)
                .fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally
        ) {
            val vm: PublicCourseViewModel = viewModel()
            val uidata by vm.uidata.collectAsState()
            Text(text = "发布者:" + MyApp.user)
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp), horizontalArrangement = Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically
            ) {
                OutlinedTextField(label = {
                    Text(text = "课程名称")
                }, value = uidata.name, onValueChange = {str->
                    vm.setuidata(uidata.copy(name = str))
                })
            }
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp), Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically
            ) {
                OutlinedTextField(label = {
                    Text(text = "学时")
                }, value = uidata.studyhour, onValueChange = { it ->
                    vm.setuidata(uidata.copy(studyhour = it))
                })
            }
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp), Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically
            ) {
                OutlinedTextField(label = {
                    Text(text = "学分")
                }, value = uidata.credits, onValueChange = { it ->
                    vm.setuidata(uidata.copy(credits = it))
                })
            }
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 10.dp), Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically
            ) {
                OutlinedTextField(label = {
                    Text(text = "考察方式")
                }, value = uidata.exam, onValueChange = {str->
                    vm.setuidata(uidata.copy(exam = str))
                })
            }
            Button(modifier = Modifier
                .padding(top = 20.dp)
                .width(200.dp), onClick = {
                vm.public()
            }) {
                Text(text = "发布")
            }
        }
    }

}